<template>
  <div class="login-container">
    <!-- 粒子背景 -->
    <div id="particles-js"></div>

    <div class="login-box">
      <h1>拓件智录一体平台</h1>
      <el-form :model="user" :rules="rules" ref="loginRef" class="login-form">
        <el-form-item prop="username">
          <el-input
              prefix-icon="el-icon-user"
              placeholder="请输入账号"
              v-model="user.username"
              :style="inputStyle"
              clearable
              size="large"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              type="password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              v-model="user.password"
              clearable
              size="large"
              :style="inputStyle"
          />
        </el-form-item>
        <el-form-item prop="code">
          <div style="display: flex">
            <el-input placeholder="请输入验证码" prefix-icon="el-icon-circle-check" size="large" style="flex: 1" v-model="user.code"></el-input>
            <div style="flex: 1; height: 36px">
              <valid-code @update:value="getCode" />
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
              class="login-button"
              type="primary"
              @click="login"
              :loading="loading"
              :disabled="loading || !user.username || !user.password"
          >
            登 录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import ValidCode from "@/conponents/ValidCode";
export default {
  name: "Login",
  components: {ValidCode},
  data() {
    // 验证码校验
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code) {
        console.log(this.code)
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }
    return {
      code: '', // 验证码
      user: {
        username: '',
        password: '',
        code: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        code: [
          { validator: validateCode, trigger: 'blur' }
        ],
      },
      loading: false
    };
  },
  mounted() {
    // 确保 window.particlesJS 存在再加载粒子效果
    if (window.particlesJS) {
      particlesJS.load("particles-js", "/particles.json", function () {
        console.log("Particles.js loaded");
      });
    } else {
      console.error("Particles.js 加载失败，请检查网络或CDN地址");
    }
  },
  methods: {
    login() {
      this.$refs.loginRef.validate((valid) => {
        if (valid) {
          this.loading = true;
          // 模拟登录请求
          this.$request.post('/Web/login', this.user).then(res => {
            if (res.code === '200') {
              this.loading = false;
              this.$router.push('/')
              this.$message.success('登录成功')
              localStorage.setItem("honey-user", JSON.stringify(res.data))  // 存储用户数据
            } else {
              this.$message.error(res.msg)
              this.loading = false;
            }
          })
        }
      });
    },
    getCode(code) {
      this.code = code.toLowerCase()
    },
  },
  computed: {
    inputStyle() {
      return {
        width: '100%',
        fontSize: '14px',
        marginBottom: '20px'
      };
    }
  }
};
</script>

<style scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: radial-gradient(circle at center, #409EFF, #409EFF);
  overflow: hidden;
}

#particles-js {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.login-box {
  position: relative;
  z-index: 10;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(15px);
  padding: 50px;
  border-radius: 12px;
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.2);
  width: 500px; /* 增大宽度 */
  text-align: center;
}

h1 {
  color: white;
  font-size: 40px;
  font-weight: bold;
  margin-bottom: 30px;
}

.login-form {
  width: 100%;
}

.login-button {
  width: 100%;
  height: 40px;
  background: linear-gradient(90deg, #1e90ff, #1e90ff);
  color: white;
  font-size: large;
  font-weight: bold;
  border-radius: 5px;
  transition: all 0.3s ease;
}

.login-button:hover {
  box-shadow: 0 0 15px rgba(0, 195, 255, 0.7);
  transform: scale(1.05);
}


@media (max-width: 600px) {
  .login-box {
    padding: 20px;
    width: 90%;
  }

  h1 {
    font-size: 20px;
  }

  .login-button {
    font-size: 14px;
  }
}
</style>
